package com.itheima.stock.service.Impl;

import com.itheima.stock.cache.Cache;
import com.itheima.stock.constant.Constants;
import com.itheima.stock.dto.LoginDto;
import com.itheima.stock.entity.StockMarketIndexInfo;
import com.itheima.stock.entity.StockMarketLogPrice;
import com.itheima.stock.entity.SysUser;
import com.itheima.stock.enums.CodeEnum;
import com.itheima.stock.exception.Exceptions;
import com.itheima.stock.mapper.StockMarketIndexInfoMapper;
import com.itheima.stock.mapper.StockMarketLogPriceMapper;
import com.itheima.stock.mapper.SysUserMapper;

import com.itheima.stock.service.UserService;
import com.itheima.stock.util.DateTimeUtil;
import com.itheima.stock.util.IdWorkers;
import com.itheima.stock.util.RedisKeyUtil;
import com.itheima.stock.vo.CaptchaVo;
import com.itheima.stock.vo.InnerMarketVO;
import com.itheima.stock.vo.LoginVo;
import com.itheima.stock.vo.MarketIndexVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private SysUserMapper sysUserMapper;
    private static final int Four = 4;
    private final Cache cache;
    @Autowired
    private PasswordEncoder passwordEncoder;

    public UserServiceImpl(Cache cache, IdWorkers idWorkers) {
        this.cache = cache;
        this.idWorkers = idWorkers;
    }
    private final IdWorkers idWorkers;

    @Override
    public CaptchaVo getCaptcha() {
        String captcha = RandomStringUtils.randomNumeric(Four);
        //生成redis key key =前缀+后缀（雪花算法）
        String redisKey = RedisKeyUtil.generateKey(Constants.CAPTCHA_PREFIX, idWorkers.nextStrId());
        //把验证码存进redis
        cache.set(redisKey,captcha,Constants.CAPTCHA_EXPIRE_TIME);
        return CaptchaVo.builder()
                .rkey(redisKey)
                .code(captcha).build();
    }

    @Override
    public LoginVo login(LoginDto loginDto) {
        //参数校验
        if(ObjectUtils.isEmpty(loginDto.getUsername())
        || ObjectUtils.isEmpty(loginDto.getPassword())
        || ObjectUtils.isEmpty(loginDto.getCode())
        ||ObjectUtils.isEmpty(loginDto.getRkey())){
            Exceptions.cast(CodeEnum.DATA_ERROR);
        }
        //判断验证码是否正确
        String rkey = loginDto.getRkey();
        String captcha = cache.get(rkey);
        if(!captcha.equals(loginDto.getCode())){
            Exceptions.cast(CodeEnum.SYSTEM_VERIFY_CODE_ERROR);
        }
        //判断用户是否存在密码状态是否正确
        SysUser user = sysUserMapper.select(loginDto.getUsername(),Constants.SYS_STATUS_NORMAL,Constants.SYS_STATUS_NOT_DELETE);
        if(ObjectUtils.isEmpty(user) || !passwordEncoder.matches(loginDto.getPassword(),user.getPassword())){
            Exceptions.cast(CodeEnum.SYSTEM_USERNAME_NOT_EXISTS);

        }
        //封装数据
        return LoginVo.builder()
                .id(user.getId())
                .phone(user.getPhone())
                .nickName(user.getNickName())
                .username(user.getUsername())
                .build();
    }


}

